<?php
/**
 * Fang_DAO_Article
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-03-09 23:14:08
 * 修改记录: 
 * 
 * $Id: Article.php 18 2011-04-17 12:50:07Z coosuu $
 */

class Fang_DAO_Article extends DAO_Mysql {

    const STATUS_NORMAL = 1;

    const STATUS_REMOVE = 9;

    private static $_table = 'fang_articles';

    /**
     * getInfo 
     * 
     * @param  mixed $aid 
     * @param  array $fields 
     * @return void
     */
    public static function getInfo($aid, $fields = array()) {

        $conditions = 'aid = ?';
        $params = array($aid);
        return parent::getRow(self::$_table, $conditions, $params, $fields);
    }

    /**
     * add
     * 插入 
     * 
     * @param  array $data 
     * @return boolean
     */
    public static function add($data) {

        if (!$data['a_created']) {
            $data['a_created'] = date('Y-m-d H:i:s');
        }

        if (!$data['a_status']) {
            $data['a_status'] = self::STATUS_NORMAL;
        }

        return parent::add(self::$_table, $data);
    }

    /**
     * update 
     * 更新
     * 
     * @param  integer $aid 
     * @param  array $data 
     * @return boolean 
     */
    public static function update($aid, $data) {

        if (!$data['a_updated']) {
            $data['a_updated'] = date('Y-m-d H:i:s');
        }

        return parent::updateByKey(self::$_table, $data, $aid);
    }

    /**
     * remove
     * 删除 
     * 
     * @param  integer $aid 
     * @return boolean
     */
    public static function remove($aid) {

        $data = array('a_deleted' => date('Y-m-d H:i:s'),
                      'a_status' => self::STATUS_REMOVE
                      );
        return self::update($aid, $data);
    }

    /**
     * listAll 
     * 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public static function listAll(&$pageOptions = array(), $fields = array()) {

        $conditions = 'a_status != ?';
        $params = array(self::STATUS_REMOVE);
        return parent::getAll(self::$_table, $conditions, $params, $fields, $pageOptions, array('aid' => 'DESC'));
    }

    /**
     * countAll 
     * 
     * @return void
     */
    public static function countAll() {

        return parent::count(self::$_table);
    }

    /**
     * countByCid 
     * 
     * @param  mixed $cid 
     * @return void
     */
    public static function countByCid($cid) {

        $conditions = 'cid = ?';
        $params = array($cid);
        return parent::count(self::$_table, $conditions, $params);
    }

    /**
     * listByCid 
     * 
     * @param  mixed $cid 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public static function listByCid($cid, &$pageOptions = array(), $fields = array()) {

        $conditions = 'cid = ?';
        $params = array($cid);
        return parent::getAll(self::$_table, $conditions, $params, $fields, $pageOptions, array('aid' => 'DESC'));
    }

    /**
     * listByIds 
     * 
     * @param  mixed $ids 
     * @param  array $fields 
     * @return void
     */
    public static function listByIds ($ids, $fields = array()) {

        $params = array();
        $conditions = 'aid IN (' . parent::buildInSQLClause($ids, $params) . ') AND a_status != ?';
        $params[] = self::STATUS_REMOVE;
        return parent::getAll(self::$_table, $conditions, $params, $fields);
    }
}
